home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / FROMUTS / UNIXLIB37B / src / unix / c / pipe < prev    next >
Text File  |  1992-03-16  |  2KB  |  81 lines

  1. static char sccs_id[] = "@(#) pipe.c 3.1 "__DATE__" HJR";
  2.  
  3. /* pipe.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <string.h>
  8.  
  9. #include "fcntl.h"
  10.  
  11. #include "sys/unix.h"
  12. #include "sys/os.h"
  13. #include "sys/syslib.h"
  14. #include "sys/dev.h"
  15. #include "sys/param.h"
  16.  
  17. int pipe(int *p)
  18. {
  19. struct pipe *pi;
  20. struct file *f0,*f1;
  21. int fd0,fd1;
  22. char file[32];
  23. int pcnt;
  24.  
  25. if ((fd0 = __fdalloc()) < 0) return(-1);
  26. f0 = __u->file + fd0;
  27. f0->dup = f0;
  28. if ((fd1 = __fdalloc()) < 0) { f0->dup = 0; return(-1); }
  29. f1 = __u->file + fd1;
  30. f0->dup = f1;
  31. f1->dup = f0;
  32.  
  33.   {
  34.   register char *s = file;
  35.   register char *s2 = "/pipe/";
  36.   register int i;
  37.   char n[11];
  38.  
  39.   while (*s++ = *s2++); s--;
  40.   pcnt = i = __intenv("UnixLib$pcnt",0);
  41.   s2 = n + 10;
  42.   *s2-- = 0; do { *s2-- = (i % 10) + '0'; i /= 10; } while (i); s2++;
  43.   while (*s++ = *s2++);
  44.   }
  45.  
  46. f0->oflag = O_RDWR|O_CREAT|O_TRUNC|O_PIPE;
  47.  
  48.   {
  49.   int i;
  50.  
  51.   if ((i = (*(__dev[DEV_PIPE].open))(file,0777,f0)) < 0)
  52.     { f0->dup = f1->dup = 0; return(-1); }
  53.   f0->dev = f1->dev = makedev(DEV_PIPE,i);
  54.   }
  55.  
  56.   {
  57.   int r[10];
  58.   int v[1];
  59.  
  60.   v[0] = pcnt + 1;
  61.   r[0] = (int)"UnixLib$pcnt"; r[1] = (int)v; r[2] = 4; r[3] = 0; r[4] = 1;
  62.   os_swi(0x24,r);
  63.   }
  64.  
  65. f0->oflag = O_RDONLY|O_PIPE;
  66. f1->oflag = O_WRONLY|O_PIPE;
  67.  
  68. f0->pid = f1->pid = __u->pid;
  69.  
  70. if (!(pi = malloc(sizeof(struct pipe))))
  71.   { close(fd0); close(fd1); return(-1); }
  72. pi->p[0] = f0; pi->p[1] = f1;
  73. if (!(pi->file = __permstr(file)))
  74.   { close(fd0); close(fd1); free(pi); return(-1); }
  75. pi->next = __pipe; __pipe = pi;
  76.  
  77. p[0] = fd0; p[1] = fd1;
  78.  
  79. return(0);
  80. }
  81.